package test;

/*
 * Author: JacksonCoder
 * FileName: demo3
 * Version: 1.0.0
 * Date: 2025/5/5
 */

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class demo3 {
    public static void main(String[] args) {

        List<String> list = Arrays.asList("apple","banana","pear","watermelon","orange");
        System.out.println(list);
        String reslut = String.join(",",list);
        System.out.println(reslut);

        //
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list1.size(); i++) {
            sb.append(list1.get(i));
            if (i < list1.size() - 1) {
                sb.append(","); // 添加分隔符
            }
        }
        String result = sb.toString();
        System.out.println(result); // 输出: 1,2,3,4,5

        // java 8
        List<Integer> list2 = Arrays.asList(1,2,3,4,5);
        String res = list2.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(","));
        System.out.println(res);

        //method
        List<Integer> lists = Arrays.asList(1, 2, 3, 4, 5);
        String results = String.join(",", lists.stream()
                .map(String::valueOf) // 将每个元素转换为字符串
                .collect(Collectors.toList()));
        System.out.println(results); // 输出: 1,2,3,4,5
    }
}
